package web.admin.shop.category;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import web.admin.AdminPage;
import web.eshop.tree.CategoryTreeModelContainer;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.util.Arrays;


public class CategoryLeftMenuAdminPanel extends Panel {
    private LinkTree linkTree;
    @SpringBean
    CategoryTreeModelContainer modelProvider;

    public CategoryLeftMenuAdminPanel(String id, final AdminPage parent, final CategoryAdminPanel categoryAdminPanel) {
        super(id);
        linkTree = new LinkTree("linkTree", new Model((DefaultTreeModel) modelProvider.createTreeModel())) {
            @Override
            protected void onJunctionLinkClicked(AjaxRequestTarget target, Object node) {
                super.onJunctionLinkClicked(target, node);
                DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                if (!treeNode.isLeaf()) {
                    if (linkTree.getTreeState().isNodeExpanded(node)) {
                        //somehow it is working this way :X
                        linkTree.getTreeState().expandNode(node);
//                        linkTree.getTreeState().collapseNode(node);
                    } else {
//                        linkTree.getTreeState().expandNode(node);
                        linkTree.getTreeState().collapseNode(node);
                    }
                    linkTree.updateTree(target);
                } else {
                    System.out.println(Arrays.toString(((DefaultMutableTreeNode) treeNode).getUserObjectPath()));
                }
            }

            @Override
            protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
                super.onNodeLinkClicked(node, tree, target);
                System.err.println("onNodeLinkClicked");
                categoryAdminPanel.replacePanel((DefaultMutableTreeNode) node, target);
            }

        };
        linkTree.getTreeState().setAllowSelectMultiple(false);
        linkTree.setRootLess(true);
        linkTree.setOutputMarkupId(true);
        linkTree.setLinkType(BaseTree.LinkType.AJAX_FALLBACK);
        add(linkTree);

    }

    public LinkTree getLinkTree() {
        return linkTree;
    }

    public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, Object child) {
        DefaultTreeModel treeModel = (DefaultTreeModel) linkTree.getModel().getObject();
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(child);
        linkTree.getTreeState().selectNode(newNode, true);
        if (parent == null) {
            parent = (DefaultMutableTreeNode) treeModel.getRoot();
            parent.add(newNode);
        } else {
            parent.add(newNode);
        }
        if (!linkTree.getTreeState().isNodeExpanded(parent)) {
            linkTree.getTreeState().expandNode(parent);
        }
        treeModel.nodeStructureChanged(parent);
//        linkTree.updateTree();
        return newNode;
    }

    public void removeNode(DefaultMutableTreeNode categoryNode) {
        DefaultTreeModel treeModel = (DefaultTreeModel) linkTree.getModel().getObject();
        treeModel.removeNodeFromParent(categoryNode);
    }
}
